<?php
include_once('/var/www/html/new/com.inc.php');


if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆过期","code":0}');

write_logs1(json_encode($_REQUEST));

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


$did = $_REQUEST["did"]+0;
$act = $_REQUEST["act"];

$memberID  = $_SESSION['memberID'];
$memberSID = $_SESSION['sid'];

// exit;

if($act=="c"){// 供应商给下级分销商修改授信额度


	$memo  = $_REQUEST['meno'];
	$money = $_REQUEST["cmoney"] + 0;
	
	
	if($money<0) exit('{"status":"fail","msg":"授信额度需为正值"}');

	$resxml = $s->PFT_Member_Fund($did, 2, $_SESSION['sid']);
	$simxml = simplexml_load_string($resxml);
	$nowMey = $simxml->Rec[0]->UUbasecredit/100;

	// print_r(array($did, $_SESSION['memberID'], (abs($money-$nowMey)*100),($money<$nowMey?1:0),2,(int)$_SESSION['sid'],11,3,'', $memo));
	// exit;
	$rescls = $s->PFT_Member_Fund_Modify($did, $_SESSION['memberID'], (abs($money-$nowMey)*100),($money<$nowMey?1:0),2,(int)$_SESSION['sid'],11,3,'', $memo);
	if($rescls!=100) exit('{"status":"fail","msg":"授信失败"}');
	
	$sel = "select dname from pft_member where id=$did limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
	$dname = $GLOBALS['le']->f("dname");
	$ins = "insert pft_d_operation_rec set fid=$memberID,sid=$memberSID,daction='将$dname授信额度配置为$money元',rectime=now()";
	$GLOBALS['le']->query($ins);
	exit('{"status":"success","msg":"授信成功"}');
	
}elseif($act=="t"){// 供应商给下级分销商设置的 线下还款

	$memo   = $_REQUEST['meno'];
	$money  = $_REQUEST["tmoney"] + 0;
	$resxml = $s->PFT_Member_Fund($did,1,$_SESSION['memberID']);
	$simxml = simplexml_load_string($resxml);
	$nowMey = $simxml->Rec[0]->UUbasecredit/100;
	
	$daction  = ($money>0) ? 0:1;
	$money = abs($money);
	$rescls = $s->PFT_Member_Fund_Modify($did, $_SESSION['memberID'],($money*100), $daction, 1, (int)$_SESSION['sid'], 4, 2, '', $memo);

	if($rescls!=100) exit('{"status":"fail","msg":"授信失败"}');

	$sql = "select dname from pft_member where id=$did limit 1";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$ins = "insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='为".$GLOBALS['le']->f("dname")."还款".$money."元',rectime=now()";
	$GLOBALS['le']->query($ins);
	exit('{"status":"success","msg":"还款成功"}');

	
}elseif($act=="r"){// 预存/还款

	$money = $_REQUEST["money"] + 0;
	$memo  = $_REQUEST['memo'];
	
	// if($money<=0) exit('{"status":"fail","msg":"还款金额不正确"}');
	if($money<=0) exit('<script>alert("还款金额不正确");</script>');
	
	// 预存/还款账户金额判断
	$resxml = $s->PFT_Member_Fund($_SESSION['sid'],0);
	$simxml = simplexml_load_string($resxml);
	$nowMoney = $simxml->Rec[0]->UUamoney/100;
	// if($money > $nowMoney) exit('{"status":"fail","msg":"账户余额资金不足"}');
	if($money > $nowMoney) exit('<script>alert("账户余额资金不足");</script>');
	
	$memberSID = $_SESSION['sid'];
	$memberID  = $_SESSION['memberID'];
	$money_f = $money * 100;  
	// 备注整合
	$dname = $_SESSION['dname'];
	if($_SESSION['dtype']==6){
		$sql = "select dname from pft_member where id={$_SESSION['sid']} limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc();
		$dname = $GLOBALS['le']->f('dname');
	}
	
	$sql = "select dname from pft_member where id=$did limit 1";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$supplier = $GLOBALS['le']->f('dname');
	$mask = "系统备注:{$dname}[$memberSID]预存/还款,供应商:{$supplier}[$did];用户备注:";
	$mask.= ($memo) ? $memo:'无';
	
	// 减去自己的账户余额
	$resxml = $s->PFT_Member_Fund_Modify($memberSID, $memberID, $money_f, 1, 0, '', 3, 0, '', $mask);
	// if($resxml!=100) exit('{"status":"fail","msg":"预存失败"}');
	if($resxml!=100) exit('<script>alert("预存失败");</script>');
	
	// 自己加上可供应余额
	$resxml = $s->PFT_Member_Fund_Modify($memberSID, $memberID, $money_f, 0, 1, $did, 4, 2, '', $mask);
	if($resxml!=100){
		write_logs("[预存/还款]分销商$sid未加上可供应余额,供应商[$did]",'setFund_error.log');
		// exit('{"status":"fail","msg":"预存失败"}');
		exit('<script>alert("预存失败");</script>');
	}
	
	// 供应商加上账户余额
	$resxml = $s->PFT_Member_Fund_Modify($did, $memberID, $money_f, 0, 0, '', 3, 0, '', $mask);
	if($resxml==100){
		
		$ins = "insert pft_d_operation_rec set fid=$memberID,sid=$memberSID,daction='预存/还款$money元',rectime=now()";
		$GLOBALS['le']->query($ins);
		// exit('{"status":"success","msg":"预存/还款成功"}');
		exit('<script>alert("预存/还款成功");parent.window.location=window.parent.location.href;</script>');
	}
	
	write_logs("[预存/还款]分销商$memberSID,供应商未加账户余额[$did]",'setFund_error.log');
	// exit('{"status":"fail","msg":"预存失败"}');
	exit('<script>alert("预存失败");</script>');
}


function write_logs($content, $file='setFund_success.log'){
	$file = '/var/www/html/new/d/logs/'.$file;
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$content."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

function write_logs1($txt,$file="/var/www/html/new/d/api/logs/data_111.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}